package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Message;

public class MessageDao implements BaseDao {

	private final String ADD_MESSAGE = "INSERT INTO message (user_id,text,checked,time,user_login) VALUES (?,?,?,?,?)";
	private final String GET_ALL_MESSAGES = "SELECT * FROM message ORDER BY time DESC";
	private final String GET_UREAD_MESSAGES = "SELECT * FROM message WHERE checked=false ORDER BY time DESC";
	private final String GET_ALREADY_READ_MESSAGE = "SELECT * FROM message WHERE checked=true ORDER BY time DESC";
	private final String DELETE_MESSAGE_BY_ID = "DELETE FROM message WHERE id = ?";
	private final String SET_CHECKED_BY_ID = "UPDATE message SET checked=true WHERE id=?";

	private static final Logger LOG = Logger.getLogger("stdout");

	@Override
	public <T> void create(T element) {
		Message temp = (Message) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(ADD_MESSAGE)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setString(2, temp.getText());
			preparedStatement.setBoolean(3, temp.getChecked());
			preparedStatement.setTimestamp(4, temp.getTime());
			preparedStatement.setString(5, temp.getUserLogin());
			preparedStatement.executeUpdate();
			LOG.info("Message created");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> T read(Integer id) {
		return null;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(DELETE_MESSAGE_BY_ID)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {

	}

	@SuppressWarnings("unchecked")
	public <T> T getAllMessages() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Message> temp = new ArrayList<Message>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_MESSAGES)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				temp.add(Converter.<Message> fromResaultSetToObject(rs, Message.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) temp;
	}

	@SuppressWarnings("unchecked")
	public <T> T getUnreadMessages() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Message> temp = new ArrayList<Message>();
		try (PreparedStatement statement = connection.prepareStatement(GET_UREAD_MESSAGES)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				temp.add(Converter.<Message> fromResaultSetToObject(rs, Message.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) temp;
	}

	@SuppressWarnings("unchecked")
	public <T> T getAlreadyReadMessages() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Message> temp = new ArrayList<Message>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALREADY_READ_MESSAGE)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				temp.add(Converter.<Message> fromResaultSetToObject(rs, Message.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) temp;
	}

	public void setCheckedById(Integer messageId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(SET_CHECKED_BY_ID)) {
			preparedStatement.setInt(1, messageId);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}
}
